package cn.blog.service.impl;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.blog.common.LayuiTableResult;
import cn.blog.common.PublicResultJosn;
import cn.blog.dao.MatchMapper;
import cn.blog.entity.Match;
import cn.blog.entity.MatchExample;
import cn.blog.entity.MatchExample.Criteria;
import cn.blog.service.MatchService;

@Service
public class MatchServiceImpl implements MatchService {

	@Autowired
	private MatchMapper matchMapper;

	/**
	 * 添加一场比赛信息
	 * 
	 * @param match
	 * @return
	 */
	@Override
	@Transactional
	public PublicResultJosn add(Match match) {
		match.setMatchid(UUID.randomUUID().toString());
		matchMapper.insert(match);
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), match);
	}

	/**
	 * 单、批量删除比赛信息
	 * 
	 * @param ids
	 * @return
	 */
	@Override
	@Transactional
	public PublicResultJosn delete(List<String> ids) {
		MatchExample example = new MatchExample();
		Criteria criteria = example.createCriteria();
		criteria.andMatchidIn(ids);
		matchMapper.deleteByExample(example);
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
	}

	/**
	 * 更新一场比赛信息
	 * 
	 * @param match
	 * @return
	 */
	@Override
	@Transactional
	public PublicResultJosn update(Match match) {
		matchMapper.updateByPrimaryKeySelective(match);
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), "操作成功");
	}

	/**
	 * 模糊分页查询比赛信息
	 * 
	 * @param match
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@Override
	public LayuiTableResult query(Match match, Integer page, Integer pageSize) {
		LayuiTableResult resultJosn = new LayuiTableResult();
		// 构造条件
		MatchExample example = new MatchExample();
		example.setOrderByClause("matchDate ASC");
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(match.getMatchid())) {
			// 根据id模糊查找
			criteria.andMatchidLike("%" + match.getMatchid() + "%");
		} else {
			if (StringUtils.isNotBlank(match.getMatchname())) {
				criteria.andMatchnameLike("%" + match.getMatchname() + "%");
			}
			if (StringUtils.isNotBlank(match.getMatchaddfress())) {
				criteria.andMatchaddfressLike("%" + match.getMatchaddfress() + "%");
			}
		}
		// 开启分页查找
		PageHelper.startPage(page, pageSize);
		List<Match> list = matchMapper.selectByExample(example);
		PageInfo<Match> pageInfo = new PageInfo<>(list, pageSize);
		//
		resultJosn.setCode(HttpStatus.OK.value());
		resultJosn.setMsg(HttpStatus.OK.getReasonPhrase());
		resultJosn.setCount(pageInfo.getTotal());
		resultJosn.setData(pageInfo.getList());
		return resultJosn;
	}

	/**
	 * 根据比赛分页查找此比赛的所有场次
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public PublicResultJosn queryOne(String id) {
		Match match = matchMapper.selectByPrimaryKey(id);
		return new PublicResultJosn(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), match);
	}

}
